# Python dict字典方法完全攻略
print("dir(dict) =", dir(dict))
print()

## keys()、values()和items()方法
scores = {"数学": 95, "语文": 89, "英语": 90}
print("scores.keys() =", scores.keys())
print("scores.values() =", scores.values())
print("scores.items() =", scores.items())
print()

a = {"数学": 95, "语文": 90, "英语": 90}
b = list(a.keys())
print("b =", b)

a = {"数学": 95, "语文": 90, "英语": 90}
for k in a.keys():
    print(k, end = " ")
print("\n--------------------------------")
for v in a.values():
    print(v, end = " ")
print("\n--------------------------------")
for k, v in a.items():
    print("key:", k, " value:", v)
print()

## copy()方法
a = {"one": 1, "two": 2, "three": [1, 2, 3]}
b = a.copy()
print("b =", b)
# 向 a 中添加新键值对，由于 b 已经提前将 a 所有键值对都深拷贝过来，因此 a 添加新键值对，不会影响 b
a["four"] = 100
print("a =", a)
print("b =", b)
# 由于 b 和 a 共享 [1, 2, 3]（浅拷贝），因此移除 a 中列表中的元素，也会影响 b
a["three"].remove(1)
print("a =", a)
print("b =", b)
print()

## update()方法
a = {"one": 1, "two": 2, "three": 3}
a.update({"one": 4.5, "four": 9.3})
print("a =", a)

## pop()和popitem()方法
a = {"数学": 95, "语文": 89, "英语": 90, "化学": 83, "生物": 98, "物理 ": 89}
print("a =", a)
a.pop("化学")
print("a =", a)
a.popitem()
print("a =", a)
print()

## setdefault()方法
a = {"数学": 95, "语文": 89, "英语": 90}
print("a =", a)
# key不存在，指定默认值
a.setdefault("物理", 94)
print("a =", a)
# key不存在，不指定默认值
a.setdefault("化学")
print("a =", a)
# key存在，指定默认值
a.setdefault("数学", 100)
print("a =", a)




